package com.jfinal.club._admin.article;

import com.jfinal.club._admin.media.MediaAdminService;
import com.jfinal.club.common.controller.BaseController;
import com.jfinal.club.common.model.Article;
import com.jfinal.club.feedback.FeedbackService;
import com.jfinal.club.index.IndexService;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;

/**
 * @Auther: 10091
 * @Date: 2018-07-14 16:44
 * @Description: 文章管理
 */
public class ArticleAdminController extends BaseController {

    ArticleAdminService srv = ArticleAdminService.me;

    public void index() {
        Page<Article> accountPage = srv.paginate(getParaToInt("p", 1));
        setAttr("articlePage", accountPage);
        render("index.html");
    }

    /**
     * 创建
     */
    public void add() {
        setAttr("projectList", MediaAdminService.me.findAllNoLock());    // 关联项目下拉列表
        render("add_edit.html");
    }

    /**
     * 提交创建
     */
    //@Before(MyFeedbackValidator.class)
    public void save() {
       String details =  getPara("details");
        Article article = getBean(Article.class);
        Ret ret = srv.save(article,details);
        renderJson(ret);
  }

    /**
     * 修改
     */
    public void edit() {
        keepPara("p");	// 保持住分页的页号，便于在 ajax 提交后跳转到当前数据所在的页
        //setAttr("projectList", ProjectService.me.getAllProject("id, name"));    // 关联项目下拉列表
        Integer articleId = getParaToInt("id");
        setAttr("projectList", MediaAdminService.me.findAllNoLock());    // 关联项目下拉列表
        setAttr("article", srv.edit(articleId));
        setAttr("detail",srv.getArticleDetail(articleId));
        render("add_edit.html");
    }

    /**
     * 提交修改
     */
    //@Before(MyFeedbackValidator.class)
    public void update() {
        String content = getPara("content");
        Article article = getBean(Article.class);
        Ret ret = srv.update(article,content);
        renderJson(ret);
    }

    /**
     * 锁定，目前先做成使用举报量 report 值锁定
     */
    public void lock() {
        Ret ret = srv.lock(getParaToInt("id"));

        FeedbackService.me.clearHotFeedbackCache();	// 清缓存
        IndexService.me.clearCache();
        renderJson(ret);
    }

    /**
     * 解除锁定
     */
    public void unlock() {
        Ret ret = srv.unlock(getParaToInt("id"));

        FeedbackService.me.clearHotFeedbackCache();		// 清缓存
        IndexService.me.clearCache();
        renderJson(ret);
    }

    /**
     * 删除 feedback
     */
    public void delete() {
        srv.delete(getParaToInt("id"));
        renderJson(Ret.ok("msg", "f_media 删除成功"));
    }
}
